package jsyf.oa.service.wreport;

import com.alibaba.dubbo.common.utils.StringUtils;
import com.alibaba.fastjson.JSONObject;
import jsyf.com.dao.BaseJdbcDao;
import jsyf.oa.dao.wreport.Template2ModuleDao;
import jsyf.oa.entity.wreport.Template2ModuleEntity;
import jsyf.oa.facade.wreport.Template2ModuleFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by Lenovo on 2017/12/11.
 */
@Service
@Transactional(rollbackFor = Exception.class)
@com.alibaba.dubbo.config.annotation.Service(protocol = {"dubbo"})
public class Template2ModuleService implements Template2ModuleFacade {
    @Autowired
    private Template2ModuleDao dao;
    @Override
    public Boolean config(String templateId, String moduleIds) {
        dao.removeByCondition(" AND TEMPLATE_ID = ? ",new String[]{templateId});
        if (!StringUtils.isEmpty(moduleIds)) {
            List<String> list = BaseJdbcDao.stringToList(moduleIds);
            for (int i = 0; i < list.size(); i++) {
                Template2ModuleEntity entity = new Template2ModuleEntity();
                entity.setTemplateId(templateId);
                entity.setModuleId(list.get(i));
                dao.add(entity);
            }
        }
        return true;
    }

    @Override
    public List<JSONObject> getByTemplateId(String templateId) {
        return dao.getJSONListByCondition(" AND TEMPLATE_ID = ? ",new Object[]{templateId});
    }
}
